﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Web.Models.User;
using Microsoft.Practices.Unity;
using DddCms.Application.Dto.User;
using AutoMapper;
using DddCms.Application;
using DddCms.Application.Dto;

namespace Web.Controllers
{
    
    public class User2Controller : Controller
    {

        [Dependency]
        public IUserApp UserApp { get; set; }

        public ActionResult Index()
        {
            if (!String.IsNullOrWhiteSpace(User.Identity.Name))
            {
                try
                {
                    //UserDto user = UserApp.getUserByName(User.Identity.Name);
                    //return View("LoginInfo", user);
                }
                catch (Exception)
                {

                    return View();
                }
            }
            return View();
        }

       
        /// <summary>
        /// Loggin
        /// </summary>
        /// <param name="loginData"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(LoginData loginData)
        {
            LoginViewModel model = new LoginViewModel();

            if (ModelState.IsValid)
            {
                if (!UserApp.Validate(loginData.Login, loginData.Password))
                    model.ErrorMessage = MvcHtmlString.Create("Login ou mot de passe incorrect");
                else
                {
                    FormsAuthentication.SetAuthCookie(loginData.Login, loginData.Souvenir);
                    return RedirectToAction("LoginPage", "User");
                }
            }
            model.LoginData = loginData;
            return View(model);
        }
        [HttpGet]
        public ActionResult Login()
        {
            return View(new LoginViewModel());
        }

        [Authorize(Roles = "user")]
        public ActionResult LoginPage()
        {
            var model = new LoginPageViewModel();

            Mapper.CreateMap<UserUpdateDto, UserDataUpdate>();
            //model.UserDataUpdate = Mapper.Map<UserUpdateDto, UserDataUpdate>(
            //    UserApp.getUpdateUser(User));
            
            return this.View(model);
        }

        /// <summary>
        /// Créer un utilisateur
        /// </summary>
        /// <param name="userData" />
        /// <returns></returns>
        [HttpPost]
        public ActionResult CreateUser(UserData userData)
        {
            var model = new CreateUserViewModel();
            if (ModelState.IsValid)
            {
                Mapper.CreateMap<UserData, UserDto>();

                if (!UserApp.CreateUser(Mapper.Map<UserData, UserDto>(userData)))
                    model.ErrorMessage = MvcHtmlString.Create("L'utilisateur existe déjà");
                else
                {
                    var rep = RedirectToAction("Login Sucess");
                    return rep;
                }
            }
            model.UserData = userData;
            return View(model);
        }
        [HttpGet]
        public ActionResult CreateUser()
        {
            
            var model = new CreateUserViewModel();
            return View(model);
        }

        public ActionResult LoginSucess()
        {
            return View();
        }

        public ActionResult MenuUser(string CurrentOpenID)
        {
            if (!String.IsNullOrWhiteSpace(User.Identity.Name))
            {
                User.IsInRole("admin");
                ViewData["IsAdmin"] = true;
            }
            else
                ViewData["IsAdmin"] = false;

            ViewData["CurrentOpenID"] = CurrentOpenID;
            return View();
        }
    }
}
